#include "gplayer.h"
#include "ui_gplayer.h"

gplayer::gplayer(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::gplayer)
{
    currentplay=0;
    ui->setupUi(this);
    ui->playView->setModel(&playlistmodel);
    isPlaying=false;
    playlistmodel.setStringList(playlist);
    playing=new Phonon::MediaObject(this);
    playing->setTickInterval(100);
    playing->connect(playing,SIGNAL(finished()),this,SLOT(on_track_finished()));
    audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
    Phonon::Path path = Phonon::createPath(playing, audioOutput);
    ui->numVolume->setAudioOutput(audioOutput);
    ui->seekSlider->setMediaObject(playing);
    QPalette bg,bg2;
    QBrush br(Qt::TexturePattern);
    QBrush br2(Qt::TexturePattern);
    br2.setTextureImage(QImage(":/new/prefix1/bg2.bmp"));
    br.setTextureImage(QImage(":/new/prefix1/bg.bmp"));
    bg.setBrush(QPalette::Background,br);
    bg2.setBrush(QPalette::Background,br2);
    this->setAutoFillBackground(true);
    ui->widget->setAutoFillBackground(true);
    ui->widget_2->setAutoFillBackground(true);
    ui->widget->setPalette(bg);
    ui->widget_2->setPalette(bg);
    this->setPalette(bg2);
}

gplayer::~gplayer()
{
    delete ui;
    delete playing;
    delete audioOutput;
    //delete path;
}

void gplayer::on_butOpen_clicked()
{
    QStringList files;
    QFileDialog d(this);
    files=d.getOpenFileNames();
    playlist.append(files);
    playlist.removeDuplicates();
    playlistmodel.setStringList(playlist);

}

void gplayer::play(QString &file)
{
    if(!playlist.contains(file))
    {
        playlist.insert(playlist.end(),file);
        playlistmodel.setStringList(playlist);
    }
    currentplay=playlist.indexOf(file)        ;
    ui->playView->setCurrentIndex(playlistmodel.index(currentplay));
    this->setWindowTitle(file);
    playing->stop();
    playing->setCurrentSource(Phonon::MediaSource(file));
}

void gplayer::on_butPlay_clicked()
{
    if(playing!=NULL)
    {
        if(isPlaying)
        {
            playing->pause();
            ui->butPlay->setIcon(QIcon(":/new/prefix1/play_24x32.png"));
            //ui->butPlay->setText("Play");
            isPlaying=false;
        }
        else
        {
            playing->play();
            ui->butPlay->setIcon(QIcon(":/new/prefix1/pause_24x32.png"));
            //ui->butPlay->setText("Pause");
            isPlaying=true;
        }
    }

}

void gplayer::on_butStop_clicked()
{
    if(playing!=NULL)
        playing->stop();
}

void gplayer::on_playView_doubleClicked(const QModelIndex &index)
{
    QString file=index.data().toString();
    play(file);
    on_butPlay_clicked();
}

void gplayer::on_track_finished()
{
    if(!ui->loopCurrent->isChecked())
    {
        if(ui->isShuffle->isChecked())
        {
            currentplay=randInt(0,playlist.count());
        }
        else

            currentplay++;
    }

    if(currentplay<playlist.count())    //Loop current
    {
        play();
    }
    else if(ui->loopList->isChecked()) // Loop list
    {
        currentplay=0;
        play();
    }
    else
    {
        currentplay=playlist.count()-1;
    }

}

void gplayer::on_butNext_clicked()
{
    on_butStop_clicked();
    on_track_finished();
}

void gplayer::on_butPrev_clicked()
{
    on_butStop_clicked();
    if(currentplay>0)
        currentplay--;
    play();
}

void gplayer::play()
{
    if(currentplay>=0 && currentplay<playlist.count())
    {
        isPlaying=false;
        play(playlist[currentplay]);
        on_butPlay_clicked();
    }
}

void gplayer::on_butSavePlay_clicked()
{
    QFileDialog d;
    QString name=d.getSaveFileName(this);
    QFile f(name);
    f.open(QFile::WriteOnly);
    foreach(QString s,playlist)
    {
        f.write((s+"\n").toUtf8());
    }
    f.close();

}

void gplayer::on_butLoadPlay_clicked()
{
    QFileDialog d;
    QString name=d.getOpenFileName(this);
    QFile f(name);
    QTextStream str(&f);

    on_butStop_clicked();
    playlist.clear();
    f.open(QFile::ReadOnly);
    while(!str.atEnd())
    {

        playlist.append(str.readLine());
    }
    f.close();
    playlist.removeDuplicates();
    playlistmodel.setStringList(playlist);
}

void gplayer::show()
{
    if(playnow.length()>0)
    {
        play(playnow);
        on_butPlay_clicked();
    }
    this->setVisible(true);
}

void gplayer::on_butUp_clicked()
{
    QString data=ui->playView->currentIndex().data().toString();
    int i=playlist.indexOf(data);

    if(i>0)
    {
        playlist.move(i,i-1);
        playlistmodel.setStringList( playlist);
        ui->playView->setCurrentIndex(playlistmodel.index(i-1));
    }
}

void gplayer::on_buDown_clicked()
{
    QString data=ui->playView->currentIndex().data().toString();
    int i=playlist.indexOf(data);

    if(i<playlist.count()-1)
    {
        playlist.move(i,i+1);
        playlistmodel.setStringList( playlist);
        ui->playView->setCurrentIndex(playlistmodel.index(i+1));
    }
}

void gplayer::on_butRemove_clicked()
{
    playlist.removeOne(ui->playView->currentIndex().data().toString());
    playlistmodel.setStringList( playlist);
}

void gplayer::on_isShuffle_clicked()
{
  //ui->isShuffle->setText(QString("%1").arg(audioOutput->volumeDecibel()));
}
